<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../assets/js/vue.js"></script>
</head>
<body>

    <div id="app">
        <!-- 
            通过 计算属性 分解 v-model 双向数据逻辑
        -->
        <ul>
            <li><input type="checkbox" v-model="all"></li>
            <ul>
                <li v-for="(item, index) in arr" :key="index">
                    <!-- v-model 具有自动化的响应式属性增加操作 -->
                    <input type="checkbox" v-model="item.state">
                    {{ item.name }}
                    <input type="button" value="x" @click="removeUser(index)">
                </li>
            </ul>
        </ul>
        <br>
        <input type="button" value="addUser" @click="addUser()">
    </div>
    <script>
        new Vue({
            el:"#app",
            data:{
                // all:false,
                arr:[
                    { name:"Augustus",state:false },
                    { name:"Percy",state:false },
                    { name:"Sandra" }
                ]
            },
            computed:{
                all:{
                    get(){
                        // return this.arr.every(item=>item.state)
                        for (let obj of this.arr) {
                            if(!obj.state){
                                return false;
                            }  
                        }
                        return true;
                    },
                    set(nv){
                        // for (let obj of this.arr) {
                        //     // obj.state = nv;
                        //     this.$set(obj,"state",nv)
                        // }
                        for (let i in this.arr) {
                            this.$set(this.arr[i],"state",nv)
                        }
                    }
                }
            },
            methods: {
                addUser(){
                    this.arr.push({
                        name:Math.random()
                    })
                },
                removeUser(i){
                    this.arr.splice(i,1)
                }
            },
        })
    </script>
</body>
</html>